package com.qc31.baselib.utils

import android.content.Context
import android.os.Parcelable
import com.tencent.mmkv.MMKV
import java.util.*

object CacheMMKV {
    private lateinit var defaultMMKV: MMKV
    private lateinit var saveMMKV: MMKV
    private lateinit var jsonMMKV: MMKV
    fun init(application: Context) {
        //初始化MMKV
        MMKV.initialize(application, application.filesDir.absolutePath + "/mmkv_cache")
        defaultMMKV = MMKV.mmkvWithID("default", MMKV.SINGLE_PROCESS_MODE, "default_key")!!
        jsonMMKV = MMKV.mmkvWithID("json", MMKV.SINGLE_PROCESS_MODE, "json_key")!!
        saveMMKV = MMKV.mmkvWithID("cache", MMKV.SINGLE_PROCESS_MODE, "save_key")!!
    }

    //***********************************缓存数据**************************************************
    fun encode(key: String, value: Any) {
        when (value) {
            is String -> defaultMMKV.encode(key, value)
            is Int -> defaultMMKV.encode(key, value)
            is Long -> defaultMMKV.encode(key, value)
            is Float -> defaultMMKV.encode(key, value)
            is Double -> defaultMMKV.encode(key, value)
            is Boolean -> defaultMMKV.encode(key, value)
            is ByteArray -> defaultMMKV.encode(key, value)
            else -> return
        }
    }

    fun encode(key: String, value: Set<String>?) {
        value?.let {
            defaultMMKV.encode(key, value)
        }
    }

    fun decodeInt(key: String, default: Int = 0): Int {
        return defaultMMKV.decodeInt(key, default)
    }

    fun decodeDouble(key: String): Double {
        return defaultMMKV.decodeDouble(key, 0.00)
    }

    fun decodeLong(key: String, default: Long = 0L): Long {
        return defaultMMKV.decodeLong(key, default)
    }

    fun decodeBoolean(key: String, default: Boolean = false): Boolean {
        return defaultMMKV.decodeBool(key, default)
    }

    fun decodeFloat(key: String): Float {
        return defaultMMKV.decodeFloat(key, 0F)
    }

    fun decodeByteArray(key: String): ByteArray? {
        return defaultMMKV.decodeBytes(key)
    }

    fun decodeString(key: String, default: String = ""): String {
        return defaultMMKV.decodeString(key, default) ?: ""
    }

    fun decodeStringSet(key: String): Set<String>? {
        return defaultMMKV.decodeStringSet(key, Collections.emptySet())
    }

    fun removeKey(key: String) {
        defaultMMKV.removeValueForKey(key)
    }

    //清除默认所有键值
    fun clearDefAll() {
        defaultMMKV.clearAll()
    }

    fun <T : Parcelable> encodeParcelable(key: String, value: T) {
        defaultMMKV.encode(key, value)
    }

    fun <T : Parcelable> decodeParcelable(key: String, tClass: Class<T>): T? {
        return defaultMMKV.decodeParcelable(key, tClass)
    }

    fun encodeList(key: String, listJson: String) {
        jsonMMKV.removeValueForKey(key)
        jsonMMKV.encode(key, listJson)
    }

    fun decodeList(key: String): String {
        return jsonMMKV.decodeString(key) ?: ""
    }

    //***********************************保存**************************************************
    fun encodeSave(key: String, value: Any) {
        when (value) {
            is String -> saveMMKV.encode(key, value)
            is Int -> saveMMKV.encode(key, value)
            is Long -> saveMMKV.encode(key, value)
            is Float -> saveMMKV.encode(key, value)
            is Double -> saveMMKV.encode(key, value)
            is Boolean -> saveMMKV.encode(key, value)
            is ByteArray -> saveMMKV.encode(key, value)
            else -> return
        }
    }

    fun decodeIntSave(key: String): Int {
        return saveMMKV.decodeInt(key, 0)
    }

    fun decodeStringSave(key: String, default: String = ""): String {
        return saveMMKV.decodeString(key, default) ?: ""
    }

    fun decodeBooleanSave(key: String, boolean: Boolean = false): Boolean {
        return saveMMKV.decodeBool(key, boolean)
    }

    fun decodeLongSave(key: String, default: Long = 0L): Long {
        return saveMMKV.decodeLong(key, default)
    }

    fun decodeFloatSave(key: String): Float {
        return saveMMKV.decodeFloat(key, 0F)
    }

    fun decodeByteArraySave(key: String): ByteArray? {
        return saveMMKV.decodeBytes(key)
    }

    fun <T : Parcelable> encodeParcelableSave(key: String, value: T) {
        MLog.e("key==$key value=$value")
        saveMMKV.encode(key, value)
    }

    fun <T : Parcelable> decodeParcelableSave(key: String, tClass: Class<T>): T? {
        val decodeParcelable = saveMMKV.decodeParcelable(key, tClass)
        MLog.e("书水电费水电费积分====$decodeParcelable")
        return saveMMKV.decodeParcelable(key, tClass)
    }

    fun removeKeyS(key: String) {
        saveMMKV.removeValueForKey(key)
    }

    fun clearSaveAll() {
        saveMMKV.clearAll()
    }

}



